package nio.security;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Main
 * 
 * @author nio
 */
public class Main extends Object {
	private static final Logger logger = LoggerFactory.getLogger(Main.class);

	public Main() {
		super();
	}

	public void execute() {
		try {
			Config.initialize();
			String applicationInfo = Config.getProperty(Constant.APPLICATION_INFO_KEY);
			String horizontalRule = Config.getProperty(Constant.HORIZONTAL_RULE_KEY);
			String commandNotFound = Config.getProperty(Constant.COMMAND_NOT_FOUND_KEY);
			CommandLineInterfaceWriter cLIWriter = new CommandLineInterfaceWriter();

			cLIWriter.writeln(horizontalRule);
			cLIWriter.writeln(applicationInfo);
			cLIWriter.writeln(horizontalRule);
			cLIWriter.write("");
			BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

			for (String line = input.readLine(); input != null; line = input.readLine()) {
				logger.debug("Input line='" + line + "'");
				if (!(line.trim().length() > 0)) {
					// do nothing
				} else if (line.equals(Constant.EXIT_COMMAND)) {
					this.exitCommand();
				} else {
					cLIWriter.writeln(line + commandNotFound);
				}
				cLIWriter.write("");
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}

	private void exitCommand() {
		System.exit(0);
	}

	public static void main(String[] args) {
		Runnable task = new Runnable() {
			@Override
			public void run() {
				new Main().execute();
			}
		};
		new Thread(task).start();
	}

}
